package com.millstein.controller;

import com.millstein.properties.AlipayProperties;
import com.millstein.request.AddOrderRequest;
import com.millstein.request.RepayOrderRequest;
import com.millstein.service.ProductOrderService;
import com.millstein.util.JsonData;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author millstein
 * @since 2021-09-17
 */
@RestController
@RequestMapping("/api/product_order/v1")
@Slf4j
@EnableConfigurationProperties(AlipayProperties.class)
public class ProductOrderController {

    @Autowired
    private ProductOrderService productOrderService;

    @PostMapping("/confirm")
    @ApiOperation("提交订单")
    public void confirmOrder(@ApiParam("订单信息") @RequestBody AddOrderRequest addOrderRequest) {
        productOrderService.confirmOrder(addOrderRequest);
    }

    @GetMapping("/get_state/{outTradeNo}")
    @ApiOperation("rpc调用-根据订单编号查询订单信息")
    public JsonData getProductOrderStateByOutTradeNo(@PathVariable("outTradeNo") String outTradeNo) {
        return JsonData.buildSuccess(productOrderService.getProductOrderStateByOutTradeNo(outTradeNo));
    }

    @PostMapping("/callback")
    @ApiOperation("支付宝回调接口")
    public String callback() {
        return productOrderService.doCallback();
    }

    @GetMapping("page_order")
    @ApiOperation("分页条件查询订单信息")
    public JsonData pageOrder(@ApiParam("当前页") @RequestParam(value = "page", defaultValue = "1") int page,
                              @ApiParam("每页记录数") @RequestParam(value = "size", defaultValue = "10") int size,
                              @ApiParam("订单状态") @RequestParam(value = "state", required = false) String state) {
        return JsonData.buildSuccess(productOrderService.pageOrder(page, size, state));
    }

    @PostMapping("/repay")
    @ApiOperation("重新支付订单")
    public void repay(@ApiParam("重新支付时的订单信息") @RequestBody RepayOrderRequest repayOrderRequest) {
        productOrderService.repay(repayOrderRequest);
    }

    @GetMapping("/submit_token")
    @ApiOperation("获取确认订单时的token值")
    public JsonData getSubmitToken() {
        return JsonData.buildSuccess(productOrderService.getSubmitToken());
    }
}

